//
//  SECaseManagerController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/5.
//  Copyright © 2016年 Alphts. All rights reserved.
//
/**
 *  病例管理VC
 */
#import "SECaseManagerController.h"
#import "XGIllnessVC.h"
#import "XGAddCaseVC.h"

#import "SEIllManagerHeadView.h"
#import "SECassDataMArr.h"
#import "SEIllManagerCellView.h"

//存储数据的路径
#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])

@interface SECaseManagerController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView * reportTabelView;

//添加病例按钮
@property (nonatomic, strong)UIButton * btn_addReport;

//病例数据数组
@property (nonatomic, strong)SECassDataMArr * cassMArray;

//人物信息headview
@property (nonatomic, strong)SEIllManagerHeadView * headView;

@end

@implementation SECaseManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置代理
    self.reportTabelView.delegate = self;
    self.reportTabelView.dataSource = self;
    
    //添加控件
    self.headView = [[SEIllManagerHeadView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 150)];
    [self.view addSubview:self.headView];
    [self.view addSubview:self.reportTabelView];
    [self.view addSubview:self.btn_addReport];
    
    self.reportTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark
#pragma mark - 点击事件
- (void)clickAddReprortBtn {
    XGIllnessVC * illnessVC = [XGIllnessVC new];
    
    //block回调,让病例管理VC插入一个新cell
    illnessVC.updateDataBlock = ^(SEIllReportModel * model){
        [self.cassMArray insertObject:model atIndex:0];

        NSIndexPath * indexpath = [NSIndexPath indexPathForItem:0 inSection:0];
        
        [self.reportTabelView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationFade];
    };
    [self.navigationController pushViewController:illnessVC animated:YES];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //设置控件约束
    [_reportTabelView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(214);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-50);
    }];
    
    [_btn_addReport makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.reportTabelView.bottom).offset(0);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
}

#pragma mark
#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.cassMArray.count;
}

- (SEIllManagerCellView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    SEIllManagerCellView * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {

        cell = [[SEIllManagerCellView alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
  //取消点击效果
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    cell.model = self.cassMArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 140;
}

#pragma mark: - 懒加载
- (UITableView *)reportTabelView {
    if (nil == _reportTabelView) {
        _reportTabelView = [[UITableView alloc]init];
        _reportTabelView.showsVerticalScrollIndicator = YES;
    }
    return _reportTabelView;
}

- (UIButton *)btn_addReport {
    if (nil == _btn_addReport) {
        _btn_addReport = [[UIButton alloc]init];
        _btn_addReport.titleLabel.font = FONT(20);
        [_btn_addReport setBackgroundColor:kThemeColor];
        [_btn_addReport setTitle:@"添加病例" forState:UIControlStateNormal];
        
        _btn_addReport.layer.cornerRadius = 4;
        _btn_addReport.layer.masksToBounds = YES;
        
        [_btn_addReport addTarget:self action:@selector(clickAddReprortBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_addReport;
}

- (SECassDataMArr *)cassMArray {
    if (nil == _cassMArray) {
        //解档,读取病例数据
          SECassDataMArr * arr = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"XGIllCaseReprot.data")];
        //如果解档出来结果为空,重新实例化
        if (arr == nil || arr.count == 0) {
            arr = (SECassDataMArr *)[NSMutableArray array];
        }
        _cassMArray = arr;
        
    }
    return _cassMArray;
}

@end










